<template>
  <base-layout
    :dialogVisible="dialogVisible"
    :dialogTitle="dialogTitle"
    @dialog-close="dialogClose"
  >
    <template #searchPanel>
      <search-panel
        :selectPer="'stock:page'"
        @search="search"
        :showSearchButton="true"
        :showExportButton="true"
        :showAddButton="false"
        @export="handleDataExport(Stock,'库存数据')"
      >
        <Job-shop v-model="form.storeId" placeholder="所属门店" />
        <el-select
          clearable
          style="width: 200px"
          v-model="form.storageId"
          placeholder="现存仓库"
        >
          <el-option
            v-for="item in storageList"
            :key="item.id"
            :value="item.id"
            :label="item.storageName"
          />
        </el-select>
        <el-select
          clearable
          style="width: 200px"
          v-model="form.partsDictId"
          placeholder="配件类别"
        >
          <el-option
            v-for="item in dict['汽车配件类型']"
            :key="item.id"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <el-input
          v-model="form.likeName"
          clearable
          placeholder="配件编号/配件名称"
          style="width: 200px"
        />
        <el-button @click="stockWarn" icon="el-icon-setting" type="primary"
          >库存预警设置</el-button
        >
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-view"
            title="查看"
            style="cursor: pointer"
            v-permission="'stock:detail'"
            @click="view(row, '编辑')"
          ></i>
        </template>
      </data-table>
    </template>
    <template #dialog>
      <StockWarnSet :id="1" />
    </template>
  </base-layout>
</template>
<script>
import baseMixin from "@/mixins/base";
import { page, changeStatus } from "@/api/after-sale/storehouseManage/inStockManage";
import { findByStore } from "@/api/after-sale/storehouseManage/warehouseManage";
import { getDict } from "@/utils";
import StockWarnSet from "./component/stockWarnSet";
export default {
  components: {
    StockWarnSet,
  },
  mixins: [
    baseMixin({
      idName: "id",
      findData: page,
      dataName: ["content", "totalRecords"],
      format(res) {
        return res.map((data) => {
          data.statusName = data.status == true ? "启用" : "禁用";
          return data;
        });
      },
    }),
  ],
  data() {
    return {
      tableColumnAttributes: [
        { name: '所属门店', value: 'storeName', type: 'text' },
        { name: '配件编号', value: 'partsCode', type: 'text' },
        { name: '配件名称', value: 'partsName', type: 'text' },
        { name: '配件类别', value: 'partsType', type: 'text' },
        { name: '配件单位', value: 'partsUnit', type: 'text' },
        { name: '配件来源', value: 'source', type: 'text' },
        { name: '现存仓库', value: 'storageName', type: 'text' },
        { name: '仓库编号', value: 'storageCode', type: 'text' },
        { name: '库存总数量', value: 'num', type: 'text' },
        { name: '预警阀门', value: 'alarmNum', type: 'text' },
        { name: '平均成本单价', value: 'avgPurchasePrice', type: 'text' },
        { name: '平均销售价', value: 'avgStoragePrice', type: 'text' },
        {
          name: '状态',
          value: 'statusName',
          type: 'switch',
          switchPer: 'stock:start',
          valueId: 'status',
          action: (row) => {
            changeStatus(row.id).then((res) => {
              this.search()
            })
          }
        }
      ],
      dict: [],
      storageList: [],
      form: {},
    };
  },
  watch: {
    "form.storeId": function (value) {
      if (value) {
        this.getWareHouse(value);
        this.dict = getDict(["汽车配件类型"], this.form.storeId);
      }
    },
  },
  methods: {
    // 查看
    view(row, type) {
      this.$router.push({
        path: "/partsStock/partsStockManage/inStockDetailManage",
        query: {
          id: row.id,
        },
      });
    },
    // 获取仓库
    async getWareHouse(id) {
      let { data } = await findByStore(id);
      this.storageList = data;
    },
    search() {
      this.searchOptions = this.form;
      this.currentPage = 1;
      this.setTableData();
    },
    // 库存预警弹窗
    stockWarn() {
      this.handleDataInsert("库存预警设置");
    },
  },
  mounted() {
    this.dict = getDict(["汽车配件类型"], this.form.storeId);
  },
};
</script>
